<?php
namespace app\admin\controller;


use app\common\controller\AdminBase;
use app\common\model\Slide as SlideModel;
use think\Request;

/**
 * 广告规则
 * Class AdRule
 * @package app\admin\controller
 */
class Slide extends AdminBase
{


    /**
     * 保存轮播图
     */
    public function save(Request $request)
    {
        $data['name']   = $request->param('Banner_name') ?: '';
        $data['link']   = $request->param('link') ?: '';
        $data['sort']   = $request->param('sort') ?: 10;
        $data['status'] = $request->param('status') ?: 0;
        $data['remark'] = $request->param('remark') ?: '';
        $data['image']  = $request->param('img_url') ?: '';

        $res = SlideModel::create($data);

        if ($res) {
            $action = "轮播图ID" . $res->id;
            $this->getLog("1", $action);
            $this->success('添加成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * @param Request $request
     * @return mixed
     * banner列表
     */
    public function index(Request $request)
    {
        $where   = [];
        $keyword = $request->param('keyword') ?: "";
        $page    = $request->param('page') ?: 1;

        if ($keyword) {
            $where['name'] = ['like', "%{$keyword}%"];
        }

        $slide_list = SlideModel::where($where)->order('sort DESC')->paginate(15, false, ['page' => $page]);

        return $this->fetch('list', ['slide_list' => $slide_list, 'keyword' => $keyword]);

    }

    /**
     * @return \think\response\View
     * 添加轮播图
     */
    public function add()
    {
        return view('add');
    }


    /**
     * 编辑轮播图
     * @param $id
     * @return mixed
     */
    public function edit(Request $request)
    {
        $id    = $request->param('id');
        $slide = SlideModel::get($id);
        return view('edit', compact('slide'));
    }


    /**
     * 更新轮播图
     * @param $id
     */
    public function update(Request $request)
    {
        $data['name']   = $request->param('Banner_name') ?: '';
        $data['link']   = $request->param('link') ?: '';
        $data['sort']   = $request->param('sort') ?: 10;
        $data['status'] = $request->param('status') ?: 0;
        $data['remark'] = $request->param('remark') ?: '';
        $data['image']  = $request->param('img_url') ?: '';

        $id = $request->param('id');

        if (SlideModel::where('id', $id)->update($data)) {
            $action = "轮播图ID" . $id;
            $this->getLog("3", $action);
            $this->success('编辑成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 删除轮播图
     * @param $id
     */
    public function delete(Request $request)
    {
        $id = $request->param('id');
        //删除多条
        if (empty($id)) {
            $ids = $request->param('ids/a');
            foreach ($ids as $k => $v) {
                SlideModel::destroy($v);
                $action = "轮播图ID" . $v;
                $this->getLog("2", $action);
            }
            $this->success('删除成功');
        } else {//单条
            if (SlideModel::destroy($id)) {
                $action = "轮播图ID" . $id;
                $this->getLog("2", $action);
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
    }

    /**
     * 更改轮播图排序
     */
    public function changeSort(Request $request)
    {
        $sort = $request->param('sort');
        if (!is_numeric($sort)) {
            $data = [
                'sort' => '您输入的不是一个整数'
            ];
            return json($data);
        }
        $data = ['sort' => $sort];
        if (SlideModel::where('id', $request->param('id'))->update($data)) {
            return json($data);
        } else {
            return json(['sort' => '失败']);
        }
    }

}